package ${package};
import ${tableClass.packageName}.${tableClass.shortClassName};
import javax.validation.Valid;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import tech.wetech.admin.web.controller.base.BaseController;
import tech.wetech.admin.common.base.Result;
import tech.wetech.admin.annotation.SystemLog;
import tech.wetech.admin.common.base.Page;
import tech.wetech.admin.common.base.PageResultSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import tech.wetech.admin.service.system.${tableClass.shortClassName}Service;
import tech.wetech.admin.service.system.OrganizationService;
import tech.wetech.admin.service.system.RoleService;

/*<#assign dateTime = .now>
${dateTime?date}
${dateTime?time}
${dateTime?string["yyyy-MM-dd HH:mm:ss"]}*/
@Controller
@RequestMapping("/${tableClass.shortClassName}")
public class ${tableClass.shortClassName}Controller extends BaseController {

        @Autowired
        private ${tableClass.shortClassName}Service ${tableClass.lowerCaseName}Service;

        @Autowired
        private RoleService roleService;

        @Autowired
        private OrganizationService organizationService;

        @RequestMapping(method = RequestMethod.GET)
        @RequiresPermissions("${tableClass.lowerCaseName}:view")
        public String toPage(Model model) {
            setCommonData(model);
            return "system/${tableClass.lowerCaseName}";
        }

        @ResponseBody
        @RequestMapping("/list")
        @RequiresPermissions("${tableClass.lowerCaseName}:view")
        public PageResultSet<${tableClass.shortClassName}> list(Page page) {
            return ${tableClass.lowerCaseName}Service.findByPage(page);
        }

        @ResponseBody
        @RequestMapping(value = "/create", method = RequestMethod.POST)
        @RequiresPermissions("${tableClass.lowerCaseName}:create")
        @SystemLog("创建${tableClass.lowerCaseName}")
        public Result create(@Valid ${tableClass.shortClassName} ${tableClass.lowerCaseName}) {
            ${tableClass.lowerCaseName}Service.create${tableClass.shortClassName}(${tableClass.lowerCaseName});
            return Result.Success();
        }

        @ResponseBody
        @RequiresPermissions("${tableClass.lowerCaseName}:update")
        @SystemLog("更新${tableClass.lowerCaseName}")
        @RequestMapping(value = "/update", method = RequestMethod.POST)
        public Result update(@Valid ${tableClass.shortClassName} ${tableClass.lowerCaseName}) {
            ${tableClass.lowerCaseName}Service.update${tableClass.shortClassName}(${tableClass.lowerCaseName});
            return Result.Success();
        }

        @ResponseBody
        @RequiresPermissions("${tableClass.lowerCaseName}:delete")
        @SystemLog("删除${tableClass.lowerCaseName}")
        @RequestMapping(value = "/delete", method = RequestMethod.POST)
        @Transactional
        public Result delete(@RequestParam("id") Long id) {
            ${tableClass.lowerCaseName}Service.delete${tableClass.shortClassName}(id);
            return Result.Success();
        }

        private void setCommonData(Model model) {
            model.addAttribute("organizationList", organizationService.findAll());
            model.addAttribute("roleList", roleService.findAll());
        }
}